# -*- coding: utf-8 -*-
# @Time    : 2023/11/20 16:14
# @Author  : kl
# @File    : conftest.py
import pytest
from pages.login_page import LoginPage

@pytest.fixture(scope="session")
def login_first(browser,base_url,pytestconfig):
    # 全局先登陆
    context = browser.new_context(base_url=base_url)
    page = context.new_page()
    login = LoginPage(page)
    login.navigate()
    LoginPage(page).login("kuanglei", "a123456")

    # 等待登录成功页面重定向
    page.wait_for_url(url='**/index.html')
    # 保存storage state 到指定的文件
    storage_path = pytestconfig.rootpath.joinpath("auth/state.json")
    # print(f"获取项目的目录: {storage_path}")
    context.storage_state(path=storage_path)
    context.close()

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, playwright, pytestconfig):
    """
    添加context 上下文参数，默认每个页面加载cookies
    :param browser_context_args:
    :param playwright:
    :return:
    """
    return {
        "storage_state": pytestconfig.rootpath.joinpath("auth/state.json"),
        **browser_context_args,
    }



